<template>
	<view class="container" :style="{ height: windowHeight + 'px' }">
		<!-- 第一块：顶部区域 -->
		<view class="header-section">
			<!-- 顶部导航栏 -->
			<view class="header-nav">
				<view class="nav-left" @click="contactMerchant">
					<text class="nav-text">联系作者</text>
				</view>
				<view class="nav-center">
					<view class="title">彩票专家</view>
					<view class="title-line"></view>
				</view>
				<view class="nav-right">
					<image class="avatar" :src="userInfo.avatar || '/static/default-avatar.png'"></image>
					<text class="username">{{userInfo.nickName || '用户名'}}</text>
				</view>
			</view>

			<!-- 中间盒子 -->
			<view class="info-box">
				<view class="info-avatar">
					<image :src="publisherInfo.avatar"></image>
					<view class="info-title">{{publisherInfo.merchantName}}</view>
				</view>
				<view class="info-content">
					<view class="info-desc">{{publisherInfo.merchantIntro}}</view>
					<view class="info-stats">
						<view class="stat-item">
							<text class="stat-value">98%</text>
							<text class="stat-label">中奖率</text>
						</view>
						<view class="stat-item">
							<text class="stat-value">5000+</text>
							<text class="stat-label">成功案例</text>
						</view>
						<view class="stat-item">
							<text class="stat-value">10年+</text>
							<text class="stat-label">专业经验</text>
						</view>
					</view>
				</view>
			</view>
		</view>

		<!-- 滚动公告 -->
		<view class="notice-section">
			<view class="notice-icon">
				<image src="/static/icon-notice.svg" mode="aspectFit"></image>
			</view>
			<view class="notice-content">
				<view class="notice-marquee-container">
					<view class="notice-marquee" :style="{ transform: `translateX(-${marqueeOffset}px)` }">
						<text class="notice-text">{{marqueeText}}</text>
						<text class="notice-text" style="margin-left: 50px;">{{marqueeText}}</text>
						<text class="notice-text" style="margin-left: 50px;">{{marqueeText}}</text>
					</view>
				</view>
			</view>
		</view>

		<!-- 第二块：Tab区域 -->
		<view class="tab-section">
			<view class="tabs">
				<view class="tab-item" :class="{ active: activeTab === 'fc3d' }" @click="switchTab('fc3d')">
					<text class="tab-text">福彩3D</text>
				</view>
				<view class="tab-item" :class="{ active: activeTab === 'paisan' }" @click="switchTab('paisan')">
					<text class="tab-text">排三</text>
				</view>
			</view>

			<!-- 开奖结果区域 -->
			<view class="lottery-result">
				<template v-if="activeTab === 'fc3d'">
					<view class="lottery-title">福彩3D第{{fc3dInfo.period}}期开奖结果</view>
					<view class="lottery-numbers">
						<view class="number" v-for="(num, index) in fc3dInfo.numbers" :key="index">{{num}}</view>
					</view>
				</template>
				<template v-else>
					<view class="lottery-title">排三第{{paisanInfo.period}}期开奖结果</view>
					<view class="lottery-numbers">
						<view class="number" v-for="(num, index) in paisanInfo.numbers" :key="index">{{num}}</view>
					</view>
				</template>
			</view>
		</view>

		<!-- 搜索框区域 -->
		<view class="search-section">
			<view class="search-box">
				<view class="search-icon">
					<svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 32 32" fill="none">
						<path d="M14.5 25C20.299 25 25 20.299 25 14.5C25 8.70101 20.299 4 14.5 4C8.70101 4 4 8.70101 4 14.5C4 20.299 8.70101 25 14.5 25Z" stroke="#FF0000" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
						<path d="M21.9242 21.9242L28 28" stroke="#FF0000" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
					</svg>
				</view>
				<input class="search-input" type="text" v-model="searchKeyword" placeholder="搜索文章标题或内容" @input="handleSearch"/>
			</view>
		</view>

		<!-- 第三块：可滑动的列表 -->
		<view class="hot-recommendations">
			<view class="section-title">
				<text class="title-text">热门推荐</text>
				<text class="title-desc">每日必中方案</text>
			</view>
			<scroll-view scroll-y="true" class="product-scroll" :style="{ height: scrollHeight + 'px' }">
				<view class="product-list">
					<view class="scroll-item" v-for="(item, index) in filteredProductList" :key="index" @click="goToDetail(item)">
						<view class="item-content">
							<view class="item-left">
								<view class="item-title">{{item.title}}</view>
								<view class="item-content-text">{{item.content}}</view>
								<view class="item-tags">
									<view class="item-tag" v-for="(tag, index) in (item.tag ? item.tag.split(',') : [])" :key="index">{{tag}}</view>
								</view>
								<view class="item-time">{{item.time}}</view>
							</view>
							<view class="item-price">{{item.price}}</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import api from '../../api/api.js';

	export default {
		data() {
			return {
				windowHeight: 0,
				activeTab: 'fc3d',
				fc3dInfo: {
					period: '2023001',
					numbers: [1, 2, 3]
				},
				paisanInfo: {
					period: '2023001',
					numbers: [4, 5, 6]
				},
				publisherInfo: {
					publisherId: "",
					userId: "",
					avatar: "",
					merchantName: "",
					merchantIntro: ""
				},
				productList: [{
						title: '3D分析推荐',
						content:'我是简介',
						tag:'hot,三连中',
						time: '2023-04-05 12:00',
						price: '¥18.8'
					},
					
				],
				noticeList: [{
						id: 1,
						content: '今日福彩3D专家预测已更新，中奖率95%以上！'
					},
					{
						id: 2,
						content: '恭喜用户138****2568昨日购买方案中得5.6万元大奖'
					},
					{
						id: 3,
						content: '新人首单立减30元，多买多省，推荐越多省越多'
					}
				],
				marqueeText: '',
				marqueeOffset: 0,
				marqueeTimer: null,
				contentHeight: 0,
				searchKeyword: '',
				filteredProductList: [],
				userInfo: {
					nickName: '',
					avatar: '',
					userId: ''
				},
				scrollHeight: 0
			}
		},
		onLoad() {
			// 页面加载时的初始化工作
			this.initMarquee();
			this.getSystemInfo();
			// 初始化筛选后的产品列表
			this.filteredProductList = [...this.productList];
			console.log("获取用户信息")
			// 获取用户信息
			this.getUserInfo();
		},
		onUnload() {
			// 清除定时器
			if (this.marqueeTimer) {
				clearInterval(this.marqueeTimer);
				this.marqueeTimer = null;
			}
		},
		methods: {
			getSystemInfo() {
				const systemInfo = uni.getSystemInfoSync();
				this.windowHeight = systemInfo.windowHeight;
				// 计算内容区域高度（窗口高度减去导航栏高度）
				// 注意：状态栏高度 + 导航栏高度 = 实际的顶部占用高度
				const navHeight = systemInfo.statusBarHeight + 44; // 状态栏 + 标题栏（约44px）
				this.contentHeight = systemInfo.windowHeight - navHeight;
				this.scrollHeight = this.contentHeight - 360 - 56 - 88 - 100 - 32 - 100 - 100;
			},
			contactMerchant() {
				// 获取用户登录状态
				const token = uni.getStorageSync('loginToken');
				if (!token) {
					uni.showToast({
						title: '请先登录',
						icon: 'none'
					});
					return;
				}

				// 确保有作者ID
				if (!this.publisherInfo.userId) {
					uni.showToast({
						title: '作者信息不存在',
						icon: 'none'
					});
					return;
				}
				
				// 获取基础URL
				const baseUrl = uni.$baseUrl || 'http://localhost:8080';
				
				// 先检查作者是否允许私聊
				uni.request({
					url: baseUrl + '/api/chat/check/permission/' + this.publisherInfo.userId,
					method: 'GET',
					header: {
						'Authorization': `Bearer ${token}`
					},
					success: (res) => {
						if (res.data.code === 200 && res.data.data.canChat) {
							// 作者允许私聊，尝试创建或获取聊天室
							uni.request({
								url: baseUrl + '/api/chat/start/' + this.publisherInfo.userId,
								method: 'GET',
								header: {
									'Authorization': `Bearer ${token}`
								},
								success: (chatRes) => {
									if (chatRes.data.code === 200) {
										// 跳转到聊天页面
										uni.navigateTo({
											url: `/pages/chat/chat?roomId=${chatRes.data.data.roomId}&roomName=${chatRes.data.data.expertName}&roomType=2`
										});
									} else {
										uni.showToast({
											title: chatRes.data.msg || '联系失败',
											icon: 'none'
										});
									}
								},
								fail: () => {
									uni.showToast({
										title: '联系失败，请稍后重试',
										icon: 'none'
									});
								}
							});
						} else {
							uni.showToast({
								title: '该作者暂不允许私聊',
								icon: 'none'
							});
						}
					},
					fail: () => {
						uni.showToast({
							title: '检查聊天权限失败',
							icon: 'none'
						});
					}
				});
			},
			switchTab(tab) {
				this.activeTab = tab
			},
			viewMoreNotice() {
				uni.showToast({
					title: '查看更多公告',
					icon: 'none'
				})
			},
			initMarquee() {
				// 拼接所有公告内容，用空格分隔
				this.marqueeText = this.noticeList.map(item => item.content).join('     ');

				// 启动跑马灯定时器
				this.marqueeTimer = setInterval(() => {
					this.marqueeOffset += 1;

					// 当第一遍文本滚动完毕，重置偏移量到起点附近
					// 使用三遍文本，确保无缝衔接
					const textWidth = this.marqueeText.length * 15; // 估算文本宽度
					if (this.marqueeOffset >= textWidth) {
						this.marqueeOffset = 0; // 重置为0，因为有三遍文本，用户不会感知到重置
					}
				}, 30);
			},
			handleSearch() {
				if (!this.searchKeyword) {
					this.filteredProductList = this.productList;
					return;
				}

				const keyword = this.searchKeyword.toLowerCase();
				this.filteredProductList = this.productList.filter(item =>
					item.title.toLowerCase().includes(keyword)
				);
			},
			async getUserInfo() {
				const that = this
				try {
					//先从本地获取
					!!uni.getStorageSync('userInfo')?that.userInfo =  JSON.parse( uni.getStorageSync('userInfo')):null;
					if (that.userInfo.userId) {
						console.log(that.userInfo.userId);
					} else {
						console.log('本地无数据，线上获取')
						//本地无数据，线上获取
						const res = await api.user.getWxUserInfo('wangwu7899')
						console.log(res)
						if (res.publisherId != -1) {
							//记录作者id
							that.publisherInfo.publisherId = res.publisherId
							that.userInfo = {
								nickName: res.nickName,
								avatar: res.avatar,
								userId: res.userId,
								publisherId: res.publisherId,
								superiorId: res.superiorId,
							};
								uni.setStorage({
									key: 'userInfo',
									data: JSON.stringify(that.userInfo),
									success: function() {
										console.log('存储个人信息success');
									}
								});
							//存储登录令牌
							uni.setStorageSync('loginToken', res.token);
							
						}
					}
				} catch (err) {
					console.error('获取用户信息失败', err);
				}
				//判断是否是内部用户
				console.log("判断是否是内部用户",that.userInfo)
				if(that.userInfo.publisherId){
					that.getPublisherInfo()
				}
			},
			//获取作者方法
			getPublisherInfo() {
				const that = this
				api.user.publisherInfo(that.userInfo.publisherId).then(res => {
					console.log("获取作者方法", res)
					if (res.code === 200) {
						this.publisherInfo.merchantName = res.data.merchantName
						this.publisherInfo.merchantIntro = res.data.merchantIntro
						this.publisherInfo.avatar = res.data.avatar
						this.publisherInfo.userId = res.data.userId
					}
				})
				that.getMainInfo()
			},
			//获取首页内容
			getMainInfo() {
				const that = this
				api.article.getList({
					status: 1,
					merchantId: this.publisherInfo.userId
				}).then(res => {
					console.log("获取首页内容", res)
					// 清空原有数据
					that.productList = [];
					res.rows.forEach(item=>{
						that.productList.push({
							id: item.id, // 添加 id 字段
							title: item.title,
							content: item.content,
							tag: item.tags,
							time: item.createTime,
							price: '¥'+(item.price/100).toFixed(2)
						})
					})
					that.filteredProductList=[...that.productList]
					console.log("获取首页内容2", res)
				})
			},
			goToDetail(item) {
				uni.navigateTo({
					url: `/pages/detail/detail?id=${item.id}`
				})
			}
		}
	}
</script>

<style lang="scss">
	.container {
		background-color: #f5f5f5;
		display: flex;
		flex-direction: column;
		overflow: hidden;
		padding: 16rpx;
		box-sizing: border-box;
		position: fixed;
		width: 100%;
		height: 100vh;
	}

	.header-section {
		height: 360rpx;
		padding: 10rpx 0;
		margin-bottom: 20rpx;
		background: linear-gradient(135deg, #FF0000, #FF4D4D);
		border-radius: 20rpx;
		box-shadow: 0 4rpx 20rpx rgba(255, 0, 0, 0.2);
	}

	.header-nav {
		display: flex;
		align-items: center;
		justify-content: space-between;
		height: 60rpx;
		margin-bottom: 10rpx;
		padding: 0 20rpx;
	}

	.nav-left,
	.nav-right {
		flex: 1;
		height: 60rpx;
		display: flex;
		align-items: center;
	}

	.nav-left {
		padding-left: 10rpx;
	}

	.nav-text {
		font-size: 28rpx;
		color: #FFFFFF;
		padding: 8rpx 20rpx;
		border-radius: 30rpx;
		background-color: rgba(255, 255, 255, 0.2);
	}

	.nav-center {
		flex: 2;
		text-align: center;
	}

	.title {
		font-size: 38rpx;
		font-weight: bold;
		color: #FFFFFF;
		letter-spacing: 2rpx;
		text-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.2);
	}

	.title-line {
		width: 100rpx;
		height: 6rpx;
		background: linear-gradient(to right, rgba(255, 255, 255, 0.1), rgba(255, 255, 255, 0.8), rgba(255, 255, 255, 0.1));
		margin: 8rpx auto 0;
		border-radius: 3rpx;
	}

	.nav-right {
		justify-content: flex-end;
		padding-right: 10rpx;
	}

	.avatar {
		width: 50rpx;
		height: 50rpx;
		border-radius: 25rpx;
		border: 2rpx solid rgba(255, 255, 255, 0.3);
		margin-right: 10rpx;
	}

	.username {
		font-size: 28rpx;
		color: #FFFFFF;
	}

	.info-box {
		display: flex;
		background-color: rgba(255, 255, 255, 0.1);
		padding: 16rpx;
		border-radius: 16rpx;
		margin: 10rpx 20rpx;
		color: #fff;
		min-height: 240rpx;
		backdrop-filter: blur(10px);
	}

	.info-avatar {
		width: 110rpx;
		margin-right: 16rpx;
		flex-shrink: 0;
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.info-avatar image {
		width: 110rpx;
		height: 110rpx;
		border-radius: 60rpx;
		border: 2rpx solid rgba(255, 255, 255, 0.3);
		box-shadow: 0 4rpx 10rpx rgba(0, 0, 0, 0.2);
		margin-bottom: 8rpx;
	}

	.info-title {
		font-size: 26rpx;
		font-weight: bold;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
		color: #FFFFFF;
		text-align: center;
		width: 110rpx;
	}

	.info-content {
		flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		min-width: 0;
		padding: 8rpx 0;
	}

	.info-desc {
		font-size: 26rpx;
		color: rgba(255, 255, 255, 0.9);
		line-height: 1.4;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 4;
		overflow: hidden;
		text-overflow: ellipsis;
		word-break: break-all;
		margin-bottom: 12rpx;
	}

	.info-stats {
		display: flex;
		justify-content: space-between;
		margin-top: auto;
		padding-top: 8rpx;
	}

	.stat-item {
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.stat-value {
		font-size: 32rpx;
		font-weight: bold;
		color: #FFFFFF;
		margin-bottom: 2rpx;
	}

	.stat-label {
		font-size: 24rpx;
		color: rgba(255, 255, 255, 0.8);
	}

	.notice-section {
		height: 56rpx;
		display: flex;
		align-items: center;
		background-color: #FFF8EE;
		padding: 6rpx 16rpx;
		border-radius: 12rpx;
		margin-bottom: 12rpx;
		border: 1rpx solid #FFEBE0;
		position: relative;
		z-index: 1;
		box-shadow: 0 2rpx 10rpx rgba(255, 0, 0, 0.1);
	}

	.notice-icon {
		width: 32rpx;
		height: 32rpx;
		margin-right: 12rpx;
		flex-shrink: 0;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.notice-icon image {
		width: 100%;
		height: 100%;
	}

	.notice-content {
		flex: 1;
		overflow: hidden;
		margin-right: 10rpx;
		position: relative;
		min-width: 0;
	}

	.notice-text {
		font-size: 24rpx;
		color: #FF0000;
		white-space: nowrap;
		font-weight: bold;
		display: inline-block;
	}

	.tab-section {
		margin-bottom: 12rpx;
		background-color: #fff;
		border-radius: 16rpx;
		padding: 6rpx;
		box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
	}

	.tabs {
		display: flex;
		border-bottom: 2rpx solid #eee;
		margin-bottom: 6rpx;
	}

	.tab-item {
		flex: 1;
		text-align: center;
		padding: 8rpx 0;
		position: relative;
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.tab-text {
		font-size: 26rpx;
		font-weight: bold;
		color: #666;
	}

	

	.tab-item.active {
		color: #FF0000;
	}

	.tab-item.active .tab-text {
		color: #FF0000;
	}


	.tab-item.active:after {
		content: '';
		position: absolute;
		bottom: -2rpx;
		left: 50%;
		transform: translateX(-50%);
		width: 60rpx;
		height: 4rpx;
		background-color: #FF0000;
		border-radius: 2rpx;
	}

	.lottery-result {
		padding: 8rpx;
		background-color: #f8f8f8;
		border-radius: 12rpx;
	}

	.lottery-title {
		font-size: 24rpx;
		text-align: center;
		color: #333;
		font-weight: bold;
	}

	.lottery-numbers {
		display: flex;
		justify-content: center;
		margin-bottom: 6rpx;
	}

	.number {
		width: 50rpx;
		height: 50rpx;
		line-height: 50rpx;
		text-align: center;
		background: linear-gradient(135deg, #FF0000, #FF4D4D);
		color: #fff;
		border-radius: 25rpx;
		margin: 0 6rpx;
		font-weight: bold;
		font-size: 26rpx;
		box-shadow: 0 4rpx 8rpx rgba(255, 0, 0, 0.2);
	}

	
	.search-section {
		padding: 10rpx 20rpx;
		background-color: #ffffff;
		margin-bottom: 12rpx;
		border-radius: 16rpx;
		box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
	}

	.search-box {
		display: flex;
		align-items: center;
		background-color: #f5f5f5;
		border-radius: 30rpx;
		padding: 6rpx 16rpx;
		border: 1rpx solid #FFE4E4;
	}

	.search-icon {
		width: 26rpx;
		height: 26rpx;
		margin-right: 8rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.search-icon svg {
		width: 100%;
		height: 100%;
	}

	.search-input {
		flex: 1;
		height: 46rpx;
		font-size: 24rpx;
		color: #333;
	}

	.hot-recommendations {
		flex: 1;
		overflow: hidden;
		display: flex;
		flex-direction: column;
		margin-bottom: 12rpx;
		background-color: #ffffff;
		border-radius: 16rpx;
		box-shadow: 0 4rpx 20rpx rgba(255, 0, 0, 0.1);
		padding: 16rpx;
	}

	.section-title {
		display: flex;
		align-items: baseline;
		margin-bottom: 12rpx;
		padding: 0 8rpx;
	}

	.title-text {
		font-size: 32rpx;
		font-weight: bold;
		color: #FF0000;
		margin-right: 12rpx;
		text-shadow: 0 2rpx 4rpx rgba(255, 0, 0, 0.1);
	}

	.title-desc {
		font-size: 24rpx;
		color: #FF6B6B;
		font-weight: 500;
	}

	.product-scroll {
		flex: 1;
		height: calc(100vh - 360rpx - 56rpx - 88rpx - 100rpx - 32rpx - 100rpx - 100rpx);
		overflow: hidden;
		box-sizing: border-box;
	}

	.product-scroll > div {
		height: 100%;
		overflow: hidden;
	}

	.product-scroll > div > div {
		height: 100%;
		overflow: hidden;
	}

	.product-list {
		padding: 8rpx;
		box-sizing: border-box;
		height: calc(100% - 120rpx);
		overflow-y: auto;
		margin-bottom: 100rpx;
	}

	.scroll-item {
		width: 100%;
		margin-bottom: 12rpx;
		background: linear-gradient(to right, #FFFFFF, #FFF5F5);
		border-radius: 16rpx;
		overflow: hidden;
		box-shadow: 0 4rpx 16rpx rgba(255, 0, 0, 0.08);
		padding: 16rpx;
		box-sizing: border-box;
		border: 1rpx solid rgba(255, 0, 0, 0.1);
		transition: all 0.3s ease;
		position: relative;
	}

	.scroll-item:before {
		content: '';
		position: absolute;
		top: 0;
		left: 0;
		width: 6rpx;
		height: 100%;
		background: linear-gradient(to bottom, #FF0000, #FF4D4D);
		border-radius: 3rpx;
	}

	.scroll-item:hover {
		transform: translateY(-2rpx);
		box-shadow: 0 8rpx 24rpx rgba(255, 0, 0, 0.15);
		border-color: rgba(255, 0, 0, 0.2);
	}

	.item-content {
		display: flex;
		width: 100%;
		position: relative;
	}

	.item-left {
		flex: 1;
		width: 100%;
		padding-right: 120rpx;
	}

	.item-title {
		font-size: 30rpx;
		font-weight: bold;
		margin-bottom: 8rpx;
		color: #333;
		display: flex;
		align-items: center;
	}

	.item-title:before {
		content: '🔥';
		margin-right: 8rpx;
		font-size: 28rpx;
	}

	.item-content-text {
		font-size: 26rpx;
		color: #666;
		line-height: 1.4;
		margin-bottom: 8rpx;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
		overflow: hidden;
	}

	.item-tags {
		display: flex;
		flex-wrap: wrap;
		gap: 8rpx;
		margin-bottom: 8rpx;
	}

	.item-tag {
		font-size: 22rpx;
		color: #FF0000;
		background: linear-gradient(135deg, rgba(255, 0, 0, 0.05), rgba(255, 77, 77, 0.1));
		padding: 4rpx 16rpx;
		border-radius: 20rpx;
		border: 1rpx solid rgba(255, 0, 0, 0.1);
		font-weight: 500;
	}

	.item-time {
		font-size: 22rpx;
		color: #999;
	}

	.item-price {
		position: absolute;
		right: 0;
		top: 50%;
		transform: translateY(-50%);
		font-size: 28rpx;
		font-weight: bold;
		padding: 6rpx 20rpx;
		background: linear-gradient(135deg, #FF0000, #FF4D4D);
		color: #FFFFFF;
		border-radius: 20rpx;
		box-shadow: 0 4rpx 12rpx rgba(255, 0, 0, 0.2);
		overflow: hidden;
	}

	.notice-marquee-container {
		flex: 1;
		overflow: hidden;
		position: relative;
		white-space: nowrap;
		padding: 0 20rpx;
		width: calc(100% - 40rpx);
	}

	.notice-marquee {
		display: inline-block;
		white-space: nowrap;
		will-change: transform;
	}
</style>